/**   
 * All rights Reserved, Designed By MiGu  
 * Copyright:    Copyright(C) 2016-2020  
 * Company       MiGu  Co., Ltd.
*/
package com.migu.spms.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.migu.spms.common.HttpTools;
import com.migu.spms.common.StringUtil;
import com.migu.spms.constant.Constant;
import com.migu.spms.dao.entity.RequireAttachmentCriteria;
import com.migu.spms.dao.entity.RequireAttachmentDO;
import com.migu.spms.dao.inf.IRequireAttachmentDO;
import com.migu.spms.service.inf.IFileAttachService;
import com.migu.spms.utils.PropertiesCopyUtil;
import com.migu.spms.utils.exception.ServiceException;
import com.migu.spms.webapi.data.FileAttachRequest;
import com.migu.spms.webapi.data.RequireAttachmentRequest;


/**   
 * FileAttachServiceImpl类
 * 项目名称:  SPMS功能需求
 * 包名称:    com.migu.spms.service.impl   
 * 类名称:    FileAttachServiceImpl.java
 * 类描述:    
 * 创建人:    wcr   
 * 创建时间:  2017年6月20日 上午10:47:47   
 */
@Transactional
@Service("fileAttachServiceImpl")
public class FileAttachServiceImpl implements IFileAttachService {

    @Resource(name = "IRequireAttachmentDO")
    private IRequireAttachmentDO requireAttachmentDO;
    
    private static int TIMEOUT = 3000;
    
    @Override
    public void deleteFileAttach(FileAttachRequest msg, String sessionId) throws Exception{
        Boolean flag = true;
        if(StringUtil.isEmpty(msg.getRequireAttachmentId())){
            String result = HttpTools.postRequest(Constant.fileControl.DELETEURL+sessionId, "",
                    JSON.toJSONString(msg), TIMEOUT);
        }else{
            RequireAttachmentCriteria record = new RequireAttachmentCriteria();
            record.setFileCode(msg.getFileCode());
            List<RequireAttachmentDO> attachList = requireAttachmentDO.selectRequireAttachment(record);
            
            if(null == attachList && attachList.size() == 0){
                throw new ServiceException("附件不存在！");
            }else{
                if(attachList.size() > 1){

                }else{
                    String result = HttpTools.postRequest(Constant.fileControl.DELETEURL+sessionId, "",
                            JSON.toJSONString(msg), TIMEOUT);
                }
                requireAttachmentDO.deleteByPrimaryKey(Long.valueOf(msg.getRequireAttachmentId()));
            }
        }
        
    }

    @Override
    public void addAttachMment(RequireAttachmentRequest msg) throws Exception {
        RequireAttachmentCriteria record = PropertiesCopyUtil.copy(msg, RequireAttachmentCriteria.class);
        requireAttachmentDO.insertSelective(record);
    }
}
